package com.huiminpay.transaction.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessCast;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.playfromApi;
import com.huiminpay.transaction.convert.PayChannelParamConvert;
import com.huiminpay.transaction.convert.PlatformChannelConvert;
import com.huiminpay.transaction.entity.AppPlatformChannel;
import com.huiminpay.transaction.entity.PayChannelParam;
import com.huiminpay.transaction.entity.PlatformChannel;
import com.huiminpay.transaction.mapper.AppPlatformChannelMapper;
import com.huiminpay.transaction.mapper.PayChannelParamMapper;
import com.huiminpay.transaction.mapper.PlatformChannelMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@org.apache.dubbo.config.annotation.Service
public class PayChannelServiceImpl implements playfromApi {

    @Autowired
    private PlatformChannelMapper platformChannelMapper;
    @Autowired
    private AppPlatformChannelMapper appPlatformChannelMapper;
    @Autowired
    private  PayChannelParamMapper payChannelParamMapper;



    /**
     * 用户创建玩应用之后，需要配置，第一步先显示服务列表
     * @return
     */
    @Override
    public List<PlatformChannelDTO> queryPlatformChannel() {
        List<PlatformChannel> platformChannels = platformChannelMapper.selectList(null);
        List<PlatformChannelDTO> platformChannelDTOS = PlatformChannelConvert.INSTANCE.listentity2listdto(platformChannels);
        return platformChannelDTOS;
    }

    /**
     * 显示完服务列表之后，用户点击按钮，应用需要跟服务绑定
     * @param appId
     * @param platformChannelCodes
     * @throws BusinessException
     */
    @Override
    public void bindPlatformChannelForApp(String appId, String platformChannelCodes) throws BusinessException {
        //根据appId和平台服务类型code查询app_platform_channel
        AppPlatformChannel appPlatformChannel = appPlatformChannelMapper.selectOne(new
                LambdaQueryWrapper<AppPlatformChannel>()
                .eq(AppPlatformChannel::getAppId, appId)
                .eq(AppPlatformChannel::getPlatformChannel, platformChannelCodes));
        //如果没有绑定则绑定
        if(appPlatformChannel ==null){
            appPlatformChannel = new AppPlatformChannel();
            appPlatformChannel.setAppId(appId);
            appPlatformChannel.setPlatformChannel(platformChannelCodes);
            appPlatformChannelMapper.insert(appPlatformChannel);
        }
    }
    /**
     * 绑定之后，图标显示为灰色
     *
     */
    @Override
    public int queryAppBindPlatformChannel(String appId, String platformChannel) throws BusinessException {
        int count = appPlatformChannelMapper.selectCount(
                new QueryWrapper<AppPlatformChannel>().lambda().eq(AppPlatformChannel::getAppId, appId)
                        .eq(AppPlatformChannel::getPlatformChannel, platformChannel));
        //已存在绑定关系返回1
        if (count > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    //根据平台服务类型获取支付渠道列表
    @Override
    public List<PayChannelDTO> findPayChane(String platformChannelCode) throws BusinessException {
        return platformChannelMapper.selectPayChannelByPlatformChannel(platformChannelCode);
    }

    //保存支付渠道参数
    @Override
    public PayChannelParamDTO savePayChannelParam(PayChannelParamDTO payChannelParam) throws BusinessException {
        Long id =finde(payChannelParam.getAppId(),payChannelParam.getPlatformChannelCode());
        payChannelParam.setAppPlatformChannelId(id);
        PayChannelParam payChannelParam1 = PayChannelParamConvert.INSTANCE.dto2entity(payChannelParam);
        payChannelParamMapper.insert(payChannelParam1);
        return PayChannelParamConvert.INSTANCE.entity2dto(payChannelParam1);

    }

    @Override
    public List<PayChannelParamDTO> findPayChannerlParm(String appId, String platformChannerl) {
        Long id=this.finde(appId,platformChannerl);
        List<PayChannelParam>payChannelParams=payChannelParamMapper.selectList(new QueryWrapper<PayChannelParam>().lambda()
        .eq(PayChannelParam::getAppPlatformChannelId,id));
        List<PayChannelParamDTO> payChannelParamDTOS= PayChannelParamConvert.INSTANCE.listentity2listdto(payChannelParams);
        return payChannelParamDTOS;
    }

    @Override
    public PayChannelParamDTO findPayChannerlParmA(String appId, String platformChannerl, String payChannerl) {
        Long id =this.finde(appId,platformChannerl);
        PayChannelParam payChannelParams=payChannelParamMapper.selectOne(new QueryWrapper<PayChannelParam>().lambda()
                 .eq(PayChannelParam::getAppPlatformChannelId,id)
                 .eq(PayChannelParam::getPayChannel,payChannerl));
        PayChannelParamDTO payChannelParamDTO= PayChannelParamConvert.INSTANCE.entity2dto(payChannelParams);
        return payChannelParamDTO;
    }

    //先获取应用跟支付配置的id
    private  Long finde(String appId,String platformChannel){
        AppPlatformChannel appPlatformChannel=appPlatformChannelMapper.selectOne(
                new LambdaQueryWrapper<AppPlatformChannel>()
                .eq(AppPlatformChannel::getAppId,appId)
                .eq(AppPlatformChannel::getPlatformChannel,platformChannel));
        if(appPlatformChannel==null){
            BusinessCast.cast(CommonErrorCode.E_200210);
        }
        Long id=appPlatformChannel.getId();
        return  id;

    }


}